package com.dongglass.student;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.net.URL;
import java.util.Vector;

public class JtableDemo extends JFrame {
    JButton jButton;

    public JtableDemo() {
        super("测试JTable");


        Vector<Vector<Object>> data = new Vector<>();

        Vector<Object> rowVector1 = new Vector<>();
        rowVector1.addElement("1");
        rowVector1.addElement("张三");
        rowVector1.addElement("no1");
        rowVector1.addElement("甘肃");
        rowVector1.addElement("1");
        rowVector1.addElement("2");
        rowVector1.addElement("3");
        rowVector1.addElement("6");

        Vector<Object> rowVector2 = new Vector<>();
        rowVector2.addElement("2");
        rowVector2.addElement("李四");
        rowVector2.addElement("no1");
        rowVector2.addElement("杭州");
        rowVector2.addElement("1");
        rowVector2.addElement("2");
        rowVector2.addElement("3");
        rowVector2.addElement("6");

        Vector<Object> rowVector3 = new Vector<>();
        rowVector3.addElement("3");
        rowVector3.addElement("king");
        rowVector3.addElement("no1");
        rowVector3.addElement("山东");
        rowVector3.addElement("1");
        rowVector3.addElement("2");
        rowVector3.addElement("3");
        rowVector3.addElement("6");

        data.addElement(rowVector1);
        data.addElement(rowVector2);
        data.addElement(rowVector3);

        //tableModel: 和jtable关联后，之后只需更新model就能把数据的变化反映到jTable
        StudentTableModel studentTableModel = StudentTableModel.assembleModel(data);

        //JTable和table关联
        JTable jTable = new JTable(studentTableModel);
        //设置表头
        JTableHeader tableHeader = jTable.getTableHeader();
        tableHeader.setFont(new Font(null, Font.BOLD, 16));
        tableHeader.setForeground(Color.RED);
        //设置表格体
        jTable.setFont(new Font(null, Font.PLAIN, 14));
        jTable.setForeground(Color.black); //设置字体颜色
        jTable.setGridColor(Color.BLACK); //设置表格线
        jTable.setRowHeight(30); //设置行高

        //设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //设置表格列的渲染方式
        Vector<String> columns = StudentTableModel.getColumns();
        StudentCellRender render = new StudentCellRender();
        for (int i = 0; i < columns.size(); i++) {
            TableColumn column = jTable.getColumn(columns.get(i));
            column.setCellRenderer(render);
            if(i == 0){
                column.setPreferredWidth(50);
                column.setMaxWidth(50);
                column.setResizable(false);
            }
        }


        JScrollPane jScrollPane = new JScrollPane(jTable);
        jButton = new JButton("我是按钮");
        Container container = getContentPane();
        container.add(jButton);
        container.add(jScrollPane);


        URL resource = JtableDemo.class.getClassLoader().getResource("image/meinv.jpg");
        Image image = new ImageIcon(resource).getImage();
        setIconImage(image);
        setSize(600, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new JtableDemo();
    }
}

class StudentCellRender extends DefaultTableCellRenderer{
    //在每一行每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        //设置隔行变色
        if(row % 2 == 0){
            setBackground(Color.LIGHT_GRAY); //偶数浅灰色
        }else{
            setBackground(Color.WHITE); //奇数白色
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER); //文字水平剧中

        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

//自定义tableModel
class StudentTableModel extends DefaultTableModel {
    static Vector<String> columns = new Vector<>();

    static {
        columns.addElement("编号");
        columns.addElement("姓名");
        columns.addElement("学号");
        columns.addElement("家乡");
        columns.addElement("语文");
        columns.addElement("数学");
        columns.addElement("英语");
        columns.addElement("总分");
    }

    private StudentTableModel() {
        super(null, columns);
    }

    private static StudentTableModel studentTableModel = new StudentTableModel();

    public static StudentTableModel assembleModel(Vector<Vector<Object>> data) {
        studentTableModel.setDataVector(data,columns);
        return studentTableModel;
    }

    public static Vector<String> getColumns() {
        return columns;
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
}
